package com.sias.springboot;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;

@SpringBootApplication
public class Application {

  public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, IOException {
    ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);


    Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");
    System.out.println(singletonObjects);
    singletonObjects.setAccessible(true);
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    Map<String, Object> map = (Map<String, Object>) singletonObjects.get(beanFactory);
    map.entrySet().stream().filter(e -> e.getKey().startsWith("component")).forEach(e -> System.out.println(e.getKey() + "=" + e.getValue()));

    Resource resource = context.getResource("classpath:application.yml");
    System.out.println(resource);

    Resource[] resources = context.getResources("classpath*:META-INF/spring.factories");
    for (Resource resource1 : resources) {
      System.out.println(resource1);
    }

    System.out.println(context.getEnvironment().getProperty("JAVA_HOME"));
    System.out.println(context.getEnvironment().getProperty("server.port"));

    Component1 component1 = (Component1) context.getBean("component1");
    component1.register();
    // context.publishEvent(new UserRegisteredEvent(context));
  }

}
